package swingxrcp.framework.client.swingx;

import java.awt.Component;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import javax.swing.JFrame;

import swingxrcp.framework.client.Client;
import swingxrcp.framework.client.context.SwingXContext;
import swingxrcp.framework.model.domain.Config;
import swingxrcp.framework.util.IconUtils;
import swingxrcp.framework.util.log.Logger;
import swingxrcp.framework.util.log.LoggerFactory;

public class JXFrame extends JFrame implements SwingX {
	/**
	 * 日志输出接口
	 */
	protected final Logger logger = LoggerFactory.getLogger(getClass());

	private static final long serialVersionUID = -3329214109047962065L;

	private boolean running = false;

	/**
	 * 默认构造方法
	 *
	 */
	public JXFrame() {
		this(null);
	}

	/**
	 *
	 * @param title
	 */
	public JXFrame(String title) {
		super(title);

		initWindow();
	}

	/**
	 * 初始化窗口属性
	 *
	 */
	public void initWindow() {
		Config config = Client.getEnvironment().getCurrentClient().getConfig();
		setTitle(config.getTitle());
		setSize(config.getWidth(), config.getHeight());
		setResizable(config.isResizable());
		if (!"".equals(config.getLogo()))
			setIconImage(IconUtils.getImage(config.getLogo()));

		if (config.isCentering())
			setLocationRelativeTo(null);

		if (config.isDefaultClose()) {
			setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		} else {
			setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
			addWindowListener(new WindowAdapter() {
				public void windowClosing(WindowEvent e) {
					if (toClosing()) {
						System.exit(0);
					}
				}
			});
		}
	}

	public boolean toClosing() {
		return true;
	}
	// TODO SwingX生命周期 -=================================================================
	public void open() {
		running = true;
		setVisible(running);
	}

	public void close() {
		running = false;
		removeAttributes(); //remove all the cache which create by this entity.
		dispose();
	}

	public boolean isRunning() {
		return running;
	}
	// TODO 处理Action事件 -=================================================================

	public void registerAction(Component component, String method) {
		if (component != null) {
		} else {
			logger.error("为组件添加Action事件驱动器,组件还没有初始化!  方法名:" + method);
		}
	}

	// TODO 处理Thread事件 -=================================================================
	/**
	 * 激活当前UI容器
	 */
	public void pageActivated() {

	}

	/**
	 * 执行先/后回调
	 */
	public void executeCallback() {

	}

	/**
	 * 前执行
	 */
	public void executeBefore() {
		/* *** 子类实现 *** */
	}

	/**
	 * 后执行
	 *
	 */
	public void executeAfter() {
		/* *** 子类实现 *** */
	}

	public void run() {
		// nothing
	}

	// TODO 处理缓存 -=================================================================
	public Object getAttribute(String key) {
		return SwingXContext.get(this.getClass().getName(), key);
	}

	public void setAttribute(String key, Object object) {
		SwingXContext.add(this.getClass().getName(), key, object, false);
	}

	public void setAttribute(String key, Object object, boolean lock) {
		SwingXContext.add(this.getClass().getName(), key, object, lock);
	}

	public void removeAttribute(String key) {
		SwingXContext.remove(this.getClass().getName(), key);
	}
	
	public void removeAttributes(){
		SwingXContext.removeCacheById(this.getClass().getName());
	}
}
